//快速提问

$(function() {

	//1.初始化咨询人信息 TODO:回调函数
	$(window).on("load", getInitadvocate);

	//2.跳转咨询人列表页面
	$('.advocate').on("click", toArchivesPage);

	//3. 提交问题
	$('.question-fast').on("click", submitQuestion);

	//4   获取档案信息
	$(window).on("load", getUserarchives);

	// 展示咨询人信息 
	function getInitadvocate() {
		var initadvocateInfo = sessionStorage.getItem("archives_info_key");
		if(initadvocateInfo != null) {
			initadvocateInfo = JSON.parse(initadvocateInfo);
			$("#advocateName").text(initadvocateInfo.name);
			$(".advocateAge").html(initadvocateInfo.age + "岁");
			$(".advocateSex").text(initadvocateInfo.sex);
			$("#archivesId").val(initadvocateInfo.archivesId);
		}
		return  initadvocateInfo==null?false:true;

	}
	//跳转档案列表
	function toArchivesPage() {
		sessionStorage.setItem("skipType", "1");
		location.href = "user-archives-list.html";
	}
	
	// 快速提问提交 
	function submitQuestion() {
		var Aname = $("#advocateName").text();
		if(Aname.trim() == "") {
			mui.alert("请选择咨询人", "提示");
			return false;
		}
		
		var questionFast = $(".question").val().replace(/\s+/g, "");
		if(questionFast.length < 10 || questionFast.length > 1000) {
			mui.alert("字数限定为10-1000个", '提示');
			return false;
		};
		var questionfilesObject = $("input[name='questionfiles']").length;
		var questionfiles = "";
		$("input[name='questionfiles']").each(function() {
			questionfiles += $(this).val() + ",";
		});

		var payAmount = $("#payAmount").val();
		var $doctorId = $("#doctor_id").val();
		var doctorName = $("#doctorName").val();
		var hosptiName = $("#hosptiName").val();
		var detail = doctorName + "-图文咨询:" + hosptiName;
		//组装快速提问表单信息
		var orderObject = {}
		orderObject.questionbody = $(".question").val();
		orderObject.questionfiles = questionfiles;
		orderObject.userid = USERID;
		orderObject.openid = OPENID;
		orderObject.doctorid = $doctorId ? $doctorId : "0";
		orderObject.payAmount = "0";
		orderObject.orderDetail = detail;
		orderObject.archivesid = $("#archivesId").val();
		
		//提交快速提问信息
		var mask = mui.createMask();
		orderObject.questionerid = USERID;
		console.log(orderObject);
		choiceDoctorModule.submitFreeOrder(orderObject, function(callBack) {
			console.log(callBack);
			var data = callBack;
			if(data.status) {
				mui.alert("问题提交成功请等待医生回复", "成功", function() {
					location.replace("../../html/inquiry/message-list.html");
				});
			} else {
				mui.alert("问题提交成功请等待医生回复!", "成功", function() {
					location.replace("../../html/inquiry/message-list.html");
				});
				return false;
			}
		});
	}
	
	/**
	 * 获取用户档案信息:
	 * 1.如果有选中的档案信息,则从session缓存中获取;否则从数据库读取
	 */
	function getUserarchives() {
		var result=getInitadvocate();
		if(result==false){
			var params = {
				userid: USERID,
				type: 1
			};
			orderPayModule.queryLastArchivesidByUserid(params, function(callBack) {
				var data = callBack;
				console.log(data);
				if(utilModule.checkUndefined(data.name) != '') {
					$("#archivesId").val(data.archivesid);
					$("#advocateName").text(data.name);
					if(data.sex == 1) {
						$(".advocateSex").text("男");
					} else {
						$(".advocateSex").text("女");
					}
					$(".advocateAge").text(data.age + "岁");
				}
	
			});
		}
		initForm(); 
	}
	
	function initForm() {
		function advocateInfor () {
			mui.alert("请先选择咨询人", "提示","好的");
			return false;
		}
		var Aname = $("#advocateName").text();
		if(utilModule.checkUndefined(Aname)==""){ 
			$(".textarea textarea").on("tap",function(event){
				event.preventDefault();
				advocateInfor();
				event.preventDefault();
			});
			$(".camera-area .thumb .addFile .add input").on("tap",function(event){
				event.preventDefault();
				advocateInfor();
				event.preventDefault();
			});		
		}	
	}

});